package com.jiyun.jiyunmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.jiyunmall.common.service.impl.CrudServiceImpl;
import com.jiyun.jiyunmall.coupon.dao.SkuFullReductionDao;
import com.jiyun.jiyunmall.coupon.dao.SkuLadderDao;
import com.jiyun.jiyunmall.coupon.dto.DiscountsDTO;
import com.jiyun.jiyunmall.coupon.dto.SkuFullReductionDTO;
import com.jiyun.jiyunmall.coupon.entity.SkuFullReductionEntity;
import com.jiyun.jiyunmall.coupon.entity.SkuLadderEntity;
import com.jiyun.jiyunmall.coupon.service.SkuFullReductionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 商品满减信息
 *
 * @author boge 71164376@qq.com
 * @since 1.0.0 2022-07-12
 */
@Service
@Transactional
public class SkuFullReductionServiceImpl extends CrudServiceImpl<SkuFullReductionDao, SkuFullReductionEntity, SkuFullReductionDTO> implements SkuFullReductionService {
    @Autowired
    SkuFullReductionDao skuFullReductionDao;
    @Autowired
    SkuLadderDao skuLadderDao;
    @Override
    public QueryWrapper<SkuFullReductionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<SkuFullReductionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Boolean saveDiscountsInfo(DiscountsDTO discountsDTO) {
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(discountsDTO,skuFullReductionEntity);
        BeanUtils.copyProperties(discountsDTO,skuLadderEntity);
        // 调用dao进行保存
        int insertReductionEntity = skuFullReductionDao.insert(skuFullReductionEntity);
        if(insertReductionEntity<=0){
            throw new RuntimeException("满减保存失败");
        }
        int insertLadderEntity = skuLadderDao.insert(skuLadderEntity);
        return insertReductionEntity>0 && insertLadderEntity>0;
    }
}